<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'ming/authenticate/mingconfig.php';
require_once 'ming/authenticate/mingoauth.php';
require_once 'ming/authenticate/OAuth.php';

/**
* Ming Id Authentication API Library
* @package Soha CMS
* @author Manh Nguyen - 02/03/2012
* @link http://game.soha.vn
*/
class Ming_Service{
	
	var $http_code = 0;
	var $is_valid = FALSE;
	var $user_info = NULL;
	var $user_id = NULL;
	
	function __construct(){			
	}

	function authenticate($oauth_callback){			
		/* Build MingOAuth object with client credentials. */
		$connection = new MingOAuth(CONSUMER_KEY, CONSUMER_SECRET);
		/* Get temporary credentials. */
		$request_token = $connection->getRequestToken($oauth_callback);
		//print_r($request_token);
		//exit();
				
		/* Save temporary credentials to session. */
		$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];		
		$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
		/* If last connection failed don't display authorization link. */
		switch ($connection->http_code) {
			case 200:
				/* Build authorize URL and redirect user to Ming. */
				$url = $connection->getAuthorizeURL($token);					
				header('Location: ' . $url);
				break;
			default:
				/* Show notification if something went wrong. */
				header('Location: '.$domain."mingConsumer/loginfail");
		}
	}
}
